﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Scoot.UI.Conversation
{
    /// <summary>
    /// Interaction logic for ConversationControl.xaml
    /// </summary>
    public partial class ConversationControl : UserControl
    {

        public delegate void SendMessageHandler(int uin, string message);
        public event SendMessageHandler Send_Message;

        public delegate void WindowHideEventHandler();
        public event WindowHideEventHandler Hide_Window;

        public ConversationControl()
        {
            InitializeComponent();
        }
        public void addMessage(string message, int uin)
        {
            foreach (DialogTab it in this.Conversations.Items)
            {
                if (it.uin == uin)
                {
                    it.addMessage(message);
                    return;
                }
            }
            DialogTab tab = new DialogTab(uin);
            tab.Close_Tab += new DialogTab.CloseTabEventHandler(tab_Close_Tab);
            tab.Send_Message += new DialogTab.SendMessageHandler(tab_Send_Message);
            Conversations.Items.Add(tab);
            tab.addMessage(message);
            Conversations.SelectedItem = tab;
        }

        void tab_Send_Message(int uin, string message)
        {
            Send_Message(uin, message);
        }

        void tab_Close_Tab(DialogTab tab)
        {
            Conversations.Items.Remove(tab);
            if (Conversations.Items.Count == 0)
            {
                Hide_Window();   
            }
        }

        public void openTab(int uin)
        {
            foreach (DialogTab it in this.Conversations.Items)
            {
                if (it.uin == uin)
                {
                    Conversations.SelectedItem = it;
                    return;
                }
            }
            DialogTab tab = new DialogTab(uin);
            tab.Close_Tab += new DialogTab.CloseTabEventHandler(tab_Close_Tab);
            tab.Send_Message += new DialogTab.SendMessageHandler(tab_Send_Message);
            Conversations.Items.Add(tab);
            Conversations.SelectedItem = tab;

        }


    }
}
